package com.lxd.filter;

import com.lxd.common.constants.ApiConstant;
import com.lxd.common.constants.CacheConstant;
import com.lxd.common.model.StandardSubmit;
import com.lxd.feign.cacheClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author Dongdong
 * @注释 策略模式的上下文
 */
@Component
//实现动态刷新 注解
@RefreshScope
@Slf4j
public class CheckFilterContext {

    // 基于IOC 将CheckFilter 拿到所有的对象 放入map中
    @Autowired
    private Map<String, CheckFilter> checkFilterMap;
    @Autowired
    private cacheClient client;

    /**
     * 校验方法，按filter顺序对对象进行校验
     *
     * @param submit 被校验对象
     */
    public void check(StandardSubmit submit) {
        //根据apikey 得到到filters
        String filters = client.hgetString(CacheConstant.CLIENT_BUSINESS + submit.getApikey(), ApiConstant.CLIENT_FILTERS);
        log.info("【策略模块】客户的校验链为filters={}", filters);
        String[] filtersArray;
        if (filters != null) {
            filtersArray = filters.split(",");
            //遍历校验链，实现不同的检验
            for (String filter : filtersArray) {
                log.info("【策略模块】即将检验={}", filter);
                if (filter==null){
                    log.info("【策略模块】={}检验功能不存在哦~", filter);
                    continue;
                }
                CheckFilter checkFilter = checkFilterMap.get(filter);

                checkFilter.check(submit);

            }
        }

    }


}
